<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
//购物车控制器
class SuccesssubController extends CommonController{
	private $model;
	private $orders;
	private $orderlist;
	private $goods;
	//构造函数
	public function __init(){
		//实例化地址表模型类
		$this->model = new \Common\Model\Address;
		//实例化订单表表模型类
		$this->orders = new \Common\Model\Uorders;
		//实例化订单列表表模型类
		$this->orderlist = new \Common\Model\Orderlist;
		//实例化商品表表模型类
		$this->goods = new \Common\Model\Goods;
		//调用CommonController里面的构造函数
		parent::__init();
	}

	//用户提交订单之后
	public function index(){
		//1.查询订单表里取出货号和商品总价
		$ordersData = $this->orders->where("user_uid = {$_SESSION['uid']}")->orderBy('buildtime','DESC')->limit(1)->find();
		$did = $ordersData['address_did'];
		$addressData = $this->model->where("did = {$did}")->find();
		//根据address_did在地址表里查出地址，再将地址中的逗号去掉
		$address = str_replace(',', '', $this->model->where("did = {$did}")->pluck('addresses'));
		View::with('address',$address);
		//将电话号码加密
		$ordersData['telephone'] = substr_replace($ordersData['telephone'], '****', 4,4);
		//分配变量
		View::with('addressData',$addressData);
		View::with('ordersData',$ordersData);
		//2.查询订单列表的信息
		//查询订单主键
		$oid= $this->orders->where("user_uid = {$_SESSION['uid']}")->orderBy('buildtime','DESC')->limit(1)->pluck('oid');
		//根据订单主键查询订单列表信息
		$olData = $this->orderlist->where("order_oid = {$oid}")->get();
		//循环向每个订单列表里加入商品名称
		foreach ($olData as $k => $v) {
			$olData[$k]['gname'] = $this->goods->where("gid = {$v['goods_gid']}")->pluck('gname');
		}
		//分配变量
		View::with('olData',$olData);
	    View::make();
	}
	//用户提交订单,往地址表，订单表，订单列表里存数据
	public function addresses(){
		if(IS_POST){
			if(!$this->orders->store()) View::error($this->orders->getError());
			View::success('订单添加成功',U('Successsub/index'));
			
		    
		}
	}
	//增加新的收获地址
	public function newaddress(){
	    if(IS_POST){
			if(!$this->orders->newstore()) View::error($this->orders->getError());
			View::success('地址添加成功',U('Refer/index'));
			
		    
		}
	}
	

}

?>







